/**
 * user 操作封装
 */
import { USER_STORE_NAME } from '@/config/setting';

/**
 * 获取缓存的 user
 */
export function getUser() {
  const userinfo = localStorage.getItem(USER_STORE_NAME);
  if (!userinfo) {
    return JSON.parse(sessionStorage.getItem(USER_STORE_NAME));
  }
  return JSON.parse(userinfo);
}

/**
 * 缓存 user
 * @param userinfo user
 * @param remember 是否永久存储
 */
export function setUser(userinfo, remember) {
  removeUser();
  if (userinfo) {
    if (remember) {
      localStorage.setItem(USER_STORE_NAME, JSON.stringify(userinfo));
    } else {
      sessionStorage.setItem(USER_STORE_NAME, JSON.stringify(userinfo));
    }
  }
}

/**
 * 移除 user
 */
export function removeUser() {
  localStorage.removeItem(USER_STORE_NAME);
  sessionStorage.removeItem(USER_STORE_NAME);
}
